//
//  WBNetworkManager.swift
//  MyXinLangWeiBo
//
//  Created by 迟浩伦 on 2016/12/31.
//  Copyright © 2016年 迟浩伦. All rights reserved.
//

import UIKit
import AFNetworking
//定义枚举 区别两个请求
enum WBHTTPMethod{

    case GET
    case POST

}


//网络管理工具
class WBNetworkManager: AFHTTPSessionManager {
    //静态区 常量 闭包 在第一次访问时执行闭包 并且将结果保存在share常量中 let 只能赋值一次 单例
    static let shared = WBNetworkManager()
    //用一个函数封装 AFN 的GET/POST 请求
    func request(method: WBHTTPMethod = .GET,URLString: String,parameters: [String: Any],completion: @escaping (_ json: Any?,_ isSuccess: Bool)->()){
        //自己定义success和failure的闭包
        let success = { (task: URLSessionDataTask, json: Any?)->() in
        
            completion(json, true)
            
        }
        let failure = { (task: URLSessionDataTask?, error: Error)->() in
        print("网络请求错误\(error)")
            completion(nil, false)
        }
        
        if method == .GET {
        get(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        }else{
    
        post(URLString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
    }

}
